package com.easy.service.impl;

import java.util.List;
import java.util.Map;

import com.easy.bean.User;
import com.easy.dao.UserDao;
import com.easy.service.Service;

public class UserService implements Service{
	private UserDao userD=new UserDao();
	
	public List<User> queryList(Map params){
		String checktext=(String)params.get("checktext");
		String selecttext=(String)params.get("selecttext");
		String sql="select * from h_user where 1=1 ";
		try {
			if((!selecttext.equals(null)) && (!checktext.equals(null))) {
			sql+="and " + selecttext + " like " + "'%"+checktext+"%' ";
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		if(params.get("limit")!=null) {
			int limit=Integer.parseInt((String)params.get("limit"));
			int page=Integer.parseInt((String)params.get("page"));
			int start=(page-1)*limit;
			sql+="limit "+start+","+limit;
		}
		return userD.querList(sql);
	}
	
	public User query(String username) {
		String sql = "select username,password from h_user where username='" + username+"'";
		return userD.query(sql);
	}

	@Override
	public Object query(Map params) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int queryCount(Map params) {
		String checktext=(String) params.get("checktext");
		String sql = "select count(*) from h_user where 1=1";
		if(checktext != null) {
		sql+=" and username like '%"+checktext+"%' ";
		sql+=" or name like '%"+checktext+"%' ";
		}
		return userD.queryCount(sql);
	}

	@Override
	public int update(Map params) {
		StringBuilder strB = new StringBuilder();
		strB.append("update h_user set ");
		strB.append("username='");
		strB.append(params.get("username"));
		strB.append("',password='");
		strB.append(params.get("password"));
		strB.append("',name='");
		strB.append(params.get("name"));
		strB.append("',id_number='");
		strB.append(params.get("id_number"));
		strB.append("',sex='");
		strB.append(params.get("sex"));
		strB.append("',birth='");
		strB.append(params.get("birth"));
		strB.append("',u_state='");
		strB.append(params.get("u_state"));
		strB.append("',email='");
		strB.append(params.get("email"));
		strB.append("',phone='");
		strB.append(params.get("phone"));
		strB.append("',role='");
		strB.append(params.get("role"));
		strB.append("',regtime='");
		strB.append(params.get("regtime"));
		strB.append("',u_integral='");
		strB.append(params.get("u_integral"));
		strB.append("' where u_id='");
		strB.append(params.get("u_id"));
		strB.append("'");
		int count = userD.update(strB.toString());
		return count;
	}

	@Override
	public int delete(Map params) {
		StringBuilder strB = new StringBuilder();
		strB.append("delete from h_user where u_id='");
		strB.append(params.get("u_id"));
		strB.append("'");
		int count = userD.delete(strB.toString());
		return count;
	}

	@Override
	public int insert(Map params) {
		StringBuilder strB = new StringBuilder();
		strB.append("insert into h_user(username,password,name,id_number,sex,birth,u_state,email,phone,role,regtime,u_integral) values(");
		strB.append("'");
		strB.append(params.get("username"));
		strB.append("','");
		strB.append(params.get("password"));
		strB.append("','");
		strB.append(params.get("name"));
		strB.append("','");
		strB.append(params.get("id_number"));
		strB.append("','");
		strB.append(params.get("sex"));
		strB.append("','");
		strB.append(params.get("birth"));
		strB.append("','");
		if(params.get("u_state")!=null) {
			strB.append(params.get("u_state"));
		}else {
			strB.append("正常");
		}
		strB.append("','");
		strB.append(params.get("email"));
		strB.append("','");
		strB.append(params.get("phone"));
		strB.append("','");
		if(params.get("u_role")!=null) {
			strB.append(params.get("role"));
		}else {
			strB.append("普通用户");
		}
		strB.append("',");
		if(params.get("regtime")!=null) {
			strB.append("'");
			strB.append(params.get("regtime"));
			strB.append("'");
		}else {
			strB.append("now()");
		}
		strB.append(",'");
		if(params.get("u_integral")!=null) {
			strB.append(params.get("u_integral"));
		}else {
			strB.append("0");
		}
		strB.append("'");
		strB.append(")");
		System.out.println(strB.toString());
		int count = userD.insert(strB.toString());
		return count;
	}
}
